/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jPaginacion.java
 *
 * Created on 19/09/2010, 01:05:30 AM
 */

package administracion.helper;

import java.util.Observable;
import java.util.Observer;

/**
 *
 * @author Administrador
 */
public class jPaginacion extends javax.swing.JPanel implements Observer{

    
    private Paginacion vo;

    public jPaginacion() {
    }

    
    /** Creates new form jPaginacion */
    public jPaginacion(Observer ob,int pagTamano) {
        vo = new Paginacion(pagTamano);
        vo.addObserver(this);
        vo.addObserver(ob);
        initComponents();
       iniciarPaginacion(false);
    }

    public void iniciarPaginacion(boolean b){
        this.jbInicio.setEnabled(b);
        this.jbAtras.setEnabled(b);
        this.jbSiguiente.setEnabled(b);
        this.jbFin.setEnabled(b);
    }

    private void refresh(){
        this.jlblMax.setText("");
        this.jlblTamano.setText("");
        this.jlblTotalCoincidencias.setText("");
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlblMax = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jlblTamano = new javax.swing.JLabel();
        jbAtras = new javax.swing.JButton();
        jbSiguiente = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jbInicio = new javax.swing.JButton();
        jbFin = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jlblTotalCoincidencias = new javax.swing.JLabel();

        jlblMax.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblMax.setForeground(new java.awt.Color(51, 51, 255));
        jlblMax.setName("jlblMax"); // NOI18N

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setForeground(new java.awt.Color(51, 51, 255));
        jLabel7.setText("/");
        jLabel7.setName("jLabel7"); // NOI18N

        jlblTamano.setFont(new java.awt.Font("Tahoma", 1, 11));
        jlblTamano.setForeground(new java.awt.Color(51, 51, 255));
        jlblTamano.setName("jlblTamano"); // NOI18N

        jbAtras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16nuvola/actions/back.png"))); // NOI18N
        jbAtras.setName("jbAtras"); // NOI18N
        jbAtras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAtrasActionPerformed(evt);
            }
        });

        jbSiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16nuvola/actions/forward.png"))); // NOI18N
        jbSiguiente.setName("jbSiguiente"); // NOI18N
        jbSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSiguienteActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 255));
        jLabel1.setText("paginas");
        jLabel1.setName("jLabel1"); // NOI18N

        jbInicio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16nuvola/actions/start.png"))); // NOI18N
        jbInicio.setName("jbInicio"); // NOI18N
        jbInicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbInicioActionPerformed(evt);
            }
        });

        jbFin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16nuvola/actions/finish.png"))); // NOI18N
        jbFin.setName("jbFin"); // NOI18N
        jbFin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbFinActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(51, 51, 255));
        jLabel2.setText("coincidencias");
        jLabel2.setName("jLabel2"); // NOI18N

        jlblTotalCoincidencias.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblTotalCoincidencias.setForeground(new java.awt.Color(51, 51, 255));
        jlblTotalCoincidencias.setName("jlblTotalCoincidencias"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jlblTamano, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jlblMax, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jbInicio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbAtras)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbSiguiente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbFin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addComponent(jlblTotalCoincidencias, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(27, 27, 27))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jlblTamano, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jlblMax, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jbInicio)
                    .addComponent(jbAtras)
                    .addComponent(jbSiguiente)
                    .addComponent(jbFin)
                    .addComponent(jLabel2)
                    .addComponent(jlblTotalCoincidencias, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbAtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAtrasActionPerformed
        this.vo.anterior();
        validaInicio();
    }//GEN-LAST:event_jbAtrasActionPerformed

   

    private void jbSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSiguienteActionPerformed
        this.vo.siguiente();
        validaFinal();
        
            
    }//GEN-LAST:event_jbSiguienteActionPerformed

    private void jbInicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbInicioActionPerformed
       this.vo.inicio();
       validaInicio();
    }//GEN-LAST:event_jbInicioActionPerformed

    private void jbFinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbFinActionPerformed
        this.vo.finish();
        validaFinal();
    }//GEN-LAST:event_jbFinActionPerformed

     private void validaFinal(){
        if(this.vo.getPagActual()==this.vo.getNum_paginas()){
             this.jbSiguiente.setEnabled(false);
             this.jbFin.setEnabled(false);
        }
           
        else
            this.jbSiguiente.setEnabled(true);
        this.jbAtras.setEnabled(true);
        this.jbInicio.setEnabled(true);
    }

    private void validaInicio(){
         if(this.vo.getPagActual()==0){
             this.jbAtras.setEnabled(false);
             this.jbInicio.setEnabled(false);
         }
             
        else
            this.jbAtras.setEnabled(true);
        this.jbSiguiente.setEnabled(true);
        this.jbFin.setEnabled(true);
    }

    public void setTotalResultados(int total_resultados){
         this.vo.setPagActual(0);
         if(total_resultados>0)
                this.iniciarPaginacion(true);
           else
               this.iniciarPaginacion(false);
           refresh();
           this.vo.setMaxResults(total_resultados);
    }

    public int getPagTamano() {
        return vo.getPagTamano();
    }

    public int getPagActual() {
        return vo.getPagActual();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JButton jbAtras;
    private javax.swing.JButton jbFin;
    private javax.swing.JButton jbInicio;
    private javax.swing.JButton jbSiguiente;
    private javax.swing.JLabel jlblMax;
    private javax.swing.JLabel jlblTamano;
    private javax.swing.JLabel jlblTotalCoincidencias;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {

        this.jlblTotalCoincidencias.setText(""+vo.getMaxResults());
        if(vo.getMaxResults()>0){
             this.jlblMax.setText(""+(vo.getNum_paginas()+1));
             this.jlblTamano.setText(""+(vo.getPagActual()+1));
             if(vo.getMaxResults()<=vo.getPagTamano()){
                 iniciarPaginacion(false);
             }
        }

        /*if(vo.getMaxResults()<=vo.getPagTamano()){
            
            if(vo.getMaxResults()>0){
                this.jlblMax.setText(""+(vo.getNum_paginas()+1));
                this.jlblTamano.setText(""+(vo.getPagActual()+1));
            }

        }else
        {
            this.jlblMax.setText(""+(vo.getNum_paginas()+1));
            this.jlblTamano.setText(""+(vo.getPagActual()+1));
        }*/
    }

}
